package com.nsgf.library.ui.workbenches;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.bean.User;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.databinding.ActivityAbnormalListBinding;
import com.nsgf.library.databinding.ActivitySearchListBinding;
import com.nsgf.library.databinding.ItemNsgfReginPatrolWorkBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.DiffUtils;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.UnitTo;

import java.util.ArrayList;
import java.util.List;

/**
 * 巡查动态异常
 */
public class PatrolAbnormalActivity extends BaseActivity {

    StateHolder mState;

    private ActivityAbnormalListBinding mBinding;

    private String type = "0";//列表类型 未巡水量 1异常
    private String date = "";
    private SimpleDataBindingAdapter mAdapter;
    private List<LibraryInfo> mDatas = new ArrayList<>();

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<LibraryInfo, ItemNsgfReginPatrolWorkBinding>(PatrolAbnormalActivity.this,
                R.layout.item_nsgf_regin_patrol_work,
                DiffUtils.getInstance().getLibraryInfoItemCallback()) {
            @Override
            protected void onBindItem(ItemNsgfReginPatrolWorkBinding binding, LibraryInfo item, RecyclerView.ViewHolder holder) {
                binding.setAlbum(item);
                binding.llItemReginPatrolWorkPeopel.removeAllViews();
                if ("0".equals(type)) {
                    binding.llItemReginPatrolWork.setVisibility(View.VISIBLE);
                    binding.llItemReginPatrolAbnormal.setVisibility(View.GONE);
                    String[] username = item.getUserNameReal().split(",");
                    for (String name : username) {

                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        params.rightMargin = 4;
                        TextView textView = new TextView(mContext);
                        textView.setText(name);
                        textView.setTextSize(10);
                        UnitTo.setBorder(mContext, R.color.color_F2F2F2, R.color.color_F2F2F2, R.color.color_86909C, textView, 10);
                        textView.setPadding(22, 12, 22, 12);
                        textView.setLayoutParams(params);
                        binding.llItemReginPatrolWorkPeopel.addView(textView);
                    }
                } else {
                    binding.llItemReginPatrolWork.setVisibility(View.GONE);
                    binding.llItemReginPatrolAbnormal.setVisibility(View.VISIBLE);
                }
            }
        };

        return new DataBindingConfig(R.layout.activity_abnormal_list, BR.vm, mState)
                .addBindingParam(BR.adapter, mAdapter)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.white));
        }

        getLifecycle().addObserver(mAccountRequester);
        mBinding = (ActivityAbnormalListBinding) getBinding();
        mState.titleBar.set(getIntent().getStringExtra("title"));
        date = getIntent().getStringExtra("date");
        type = getIntent().getStringExtra("type");

        mAccountRequester.damDailyDynamics(date);
        mAccountRequester.getLibraryList().observe(this, new Observer<ResponseStatus<List<LibraryInfo>>>() {
            @Override
            public void onChanged(ResponseStatus<List<LibraryInfo>> dataResult) {

                if (dataResult.getResponseCode() == 200) {

                    for (LibraryInfo libraryInfo : dataResult.getData()) {
                        if ("0".equals(type)) {
                            if ("0".equals(libraryInfo.getIsPatrol())) {
                                mState.patrolFeed.get().add(libraryInfo);
                            }
                        } else {
                            if (!"0".equals(libraryInfo.getAbnormalNum())) {
                                mState.patrolFeed.get().add(libraryInfo);
                            }
                        }
                    }
                } else {
                    if (dataResult.getResponseCode() == 401)
                        UserInfo.getInstance().clearUserInfo();
                    showToast(dataResult.getMsg());
                }
                mAdapter.submitList(mState.patrolFeed.get());
                mAdapter.notifyDataSetChanged();
                if (mState.patrolFeed.get().size() == 0) {
                    mBinding.notdata.setVisibility(View.VISIBLE);
                    mBinding.smartRefreshLayout.setVisibility(View.GONE);
                } else {
                    mBinding.notdata.setVisibility(View.GONE);
                    mBinding.smartRefreshLayout.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    public class ClickProxy {

        public void search() {
            Intent intent = new Intent();
            intent.putExtra("search", mState.searchContent.get());
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
